package com.hanbang.mrcm.home;

import java.util.ArrayList;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import com.hanbang.mrcm.R;
import com.hanbang.mrcm.aboutme.AboutMeActivity;
import com.hanbang.mrcm.adapter.HomeViewPageAdapter;
import com.hanbang.mrcm.base.MainBaseFragmentActivity;
import com.hanbang.mrcm.bean.HomeTypeData;
import com.hanbang.mrcm.merchant.MerchantActivity;
import com.hanbang.mrcm.myview.SlidingTabLayout;

public class HomeActivity extends MainBaseFragmentActivity {

	private SlidingTabLayout slidingTabLayout;
	private HomeViewPageAdapter homeViewPageAdapter;
	private ArrayList<HomeTypeData> homeTypeDatas = new ArrayList<HomeTypeData>();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_img_5_2);
		initView();
		setOnClickListener();
		getTabData();
	}

	/*
	 * 设置点击事件
	 */
	private void setOnClickListener() {
		ViewGroup v1 = (ViewGroup) findViewById(R.id.bottom_bar_ll1);
		ViewGroup v2 = (ViewGroup) findViewById(R.id.bottom_bar_ll2);
		ViewGroup v3 = (ViewGroup) findViewById(R.id.bottom_bar_ll3);
		setBottonButton(v1);
		v2.setOnClickListener(onClickListener);
		v3.setOnClickListener(onClickListener);
	}

	/*
	 * 初始化view
	 */
	private void initView() {
		// 设置顶部的标题
		setTitle(findViewById(R.id.top_bar_title),
				getString(R.string.botton_tab1));

		slidingTabLayout = (SlidingTabLayout) findViewById(R.id.slidingtablayout);
		ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

		// 设置ViewPager属性
		homeViewPageAdapter = new HomeViewPageAdapter(
				getSupportFragmentManager(), homeTypeDatas);
		viewPager.setOffscreenPageLimit(2);
		viewPager.setAdapter(homeViewPageAdapter);
		// 定义 SlidingTabLayout
		slidingTabLayout.setDistributeEvenly(true);
		slidingTabLayout.setSelectedIndicatorColors(getResources().getColor(
				R.color.red_shen));
		slidingTabLayout.setBackgroundColor(getResources().getColor(
				R.color.white));
		slidingTabLayout.setSelectedTextColors(R.color.black);
		slidingTabLayout.setSelectedTextDefaultColors(R.color.red_shen);
		slidingTabLayout
				.setCustomTabView(R.layout.home_type_tab, R.id.textView);
		slidingTabLayout.setViewPager(viewPager); // 加载ViewPager

	}

	private void getTabData() {
		String t[] = { "全部", "最新", "最热", "精选", "动态" };

		for (int i = 0; i < t.length; i++) {
			HomeTypeData data = new HomeTypeData(i, t[i]);
			homeTypeDatas.add(data);
			slidingTabLayout.setContentDescription(i, t[i]);
		}
		homeViewPageAdapter.notifyDataSetChanged();
		slidingTabLayout.populateTabStrip();
	}

	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent();
			switch (v.getId()) {
			case R.id.bottom_bar_ll2:
				intent.setClass(HomeActivity.this, MerchantActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			case R.id.bottom_bar_ll3:
				intent.setClass(HomeActivity.this, AboutMeActivity.class);
				intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
				startActivity(intent);
				break;
			default:
				break;
			}
		}
	};

	@Override
	protected void onResume() {
		super.onResume();
		bitmapUtils.configDefaultLoadFailedImage(R.drawable.default_img_5_2);
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
